package com.zmovie.framework.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zmovie.framework.domain.Session;
import com.zmovie.framework.service.SessionService;
import com.zmovie.framework.mapper.SessionMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
* @author lenovo
* @description 针对表【session】的数据库操作Service实现
* @createDate 2023-12-03 16:40:25
*/
@Service
public class SessionServiceImpl extends ServiceImpl<SessionMapper, Session>
    implements SessionService{
    @Autowired
    private SessionMapper sessionMapper;
    @Override
    public Page<Session> pageSession(Integer pageNo, Integer pageSize, Session session) {
        Page<Session> sessionPage = new Page<>(pageNo, pageSize);
        LambdaQueryWrapper<Session> wrapper = new LambdaQueryWrapper<>();
        //电影院
        wrapper.eq(session.getCinemaId() != null, Session::getCinemaId, session.getCinemaId());
        //电影
        wrapper.eq(session.getMovieId() != null, Session::getMovieId, session.getMovieId());
        //日期
        wrapper.eq(session.getDate() != null, Session::getDate, session.getDate());
        wrapper.orderByDesc(Session::getDate).orderByAsc(Session::getCinemaId);
        return page(sessionPage, wrapper);
    }

    @Override
    public boolean updateSession(Session session) {
        if(session.getId() == null){
            return false;
        }
        LambdaUpdateWrapper<Session> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(Session::getId, session.getId());
        //影厅状态
        wrapper.set(session.getState() != null, Session::getState, session.getState());
        return update(wrapper);
    }

    @Override
    public List<Session> listByCinemaId(Integer cinemaId) {
        Session session = new Session();
        session.setCinemaId(cinemaId);
        return listCondition(session);
    }

    @Override
    public List<Session> listCondition(Session session) {
        LambdaQueryWrapper<Session> wrapper = new LambdaQueryWrapper<>();
        //影院
        wrapper.eq(session.getCinemaId() != null, Session::getCinemaId, session.getCinemaId());
        //电影
        wrapper.eq(session.getMovieId() != null, Session::getMovieId, session.getMovieId());
        //日期
        wrapper.eq(session.getDate() != null, Session::getDate, session.getDate());
        //大于当前日期
        wrapper.gt(Session::getDate, new Date());
        wrapper.orderByAsc(Session::getDate);
        return list(wrapper);
    }

    @Override
    public List<Integer> listMovieIdsByCinemaId(Integer cinemaId) {
        return sessionMapper.listMovieIdsByCinemaId(cinemaId);
    }

    @Override
    public List<Date> listDateByCinemaIdAndMovieId(Integer cinemaId, Integer movieId) {
        return sessionMapper.listDateByCinemaIdAndMovieId(cinemaId, movieId);
    }
}




